capture log close
log using $log/AED_dataprep_08_xvars_hist, text replace
   
clear

set matsize 800
set more off

set trace off
   
   
/* ------------------------------------------------------------------------------------------------- */
/* --- Define control variables measuring the employment history before entry into unemployment ---- */
/* ------------------------------------------------------------------------------------------------- */




foreach iebversion in v901 {
	foreach out in monthly {
		
		if "`out'"=="monthly" {
		local help_out ""
		local help_mon "_monthly"
		}
		
		forvalues year = 2001/2004 {
		
				di "*****************************************************"
				di "Year: `year'"
				di c(current_time)
				di "*****************************************************"
   
							
				local yearbefore = `year'-5
				local what "`yearbefore'/`year'"
				
				forvalue jahr = `what' {
				
				use $data/eintritte_ue_`year'_`iebversion', clear
				keep iza_id iza_eintrittm iza_eintritty
				
				
				
				joinby iza_id using $data/outcome`help_mon'_y`jahr'_`iebversion', unmatched(master)
				tab _merge
				drop _merge
	
				joinby iza_id using $data/outcome_mth_y`jahr'_`iebversion', unmatched(master)
				tab _merge
				drop _merge
				
				
				gen eintritt = .
				tostring eintritt, replace
				gen help = iza_eintrittm
				tostring iza_eintritty, replace
				tostring iza_eintrittm, replace
				replace eintritt = substr(iza_eintritty,1,4) + "0" + substr(iza_eintrittm,1,2) if help < 10
				replace eintritt = substr(iza_eintritty,1,4) + substr(iza_eintrittm,1,2) if help >=10
				destring eintritt, replace
				
				
				# del; 
				keep iza_id iza_eintritty iza_eintrittm eintritt iza_beh_* iza_leh_* iza_asu_* iza_lhg_* iza_geld_beh_* 
				iza_mth1_* iza_mth2_* iza_mth3_* iza_mth4_* iza_azeit_*;
				
				reshape long iza_beh_ iza_leh_ iza_asu_ iza_lhg_ iza_geld_beh_ 
				iza_mth1_ iza_mth2_ iza_mth3_ iza_mth4_ iza_azeit_, i(iza_id) j(lfd);
				
				# del cr
				sort iza_id lfd
				
				
				saveold $data/hist_`jahr',replace
				
				}/*what*/
				
				
				local year1 = `year' - 1
				local year2 = `year' - 2
				local year3 = `year' - 3
				local year4 = `year' - 4
				local year5 = `year' - 5
				
				use $data/hist_`year',clear
				append using $data/hist_`year1'
				append using $data/hist_`year2'
				append using $data/hist_`year3'
				append using $data/hist_`year4'
				append using $data/hist_`year5'
								
				sort iza_id lfd
				keep iza_id lfd iza_eintritty iza_eintrittm iza_beh_ iza_asu_ iza_leh_ iza_lhg_ iza_azeit_ iza_geld_beh_ iza_mth1_ iza_mth2_ iza_mth3_ iza_mth4_ eintritt
				/*Drop all info after entry into unemployment*/
				by iza_id: drop if lfd>eintritt
	
	
				/*=================================================*/ 
				/*Employment status last 12 months before entry into unemployment*/
				/*=================================================*/
				
				preserve
				
				by iza_id: keep if lfd < eintritt
				by iza_id: gen help = eintritt - 100  /*100 = 1 year because of the formatting yyyymm*/
				by iza_id: keep if lfd >= help
				drop help
				
				codebook iza_id
				
				/*occupational training*/
				gen apprentice_tminus1_temp = 0   
				#delimit;
				by iza_id: replace apprentice_tminus1_temp = 1 if iza_beh_==102 & (iza_asu_~=31 & iza_asu_~=32 & iza_leh_==.)
								& (iza_mth1_==. )
								& (iza_mth2_==. )
								& (iza_mth3_==. )
								& (iza_mth4_==. ); 
				#delimit cr
				by iza_id: egen iza_apprentice_tminus1 = max(apprentice_tminus1_temp)
				drop apprentice_tminus1_temp
	
				/*Employment*/
				gen sv_tminus1_temp = 0
				#delimit;
				by iza_id: replace sv_tminus1_temp = 1 if (iza_beh_==101 |
									   iza_beh_==102 |
									   iza_beh_==103 | 
									   iza_beh_==105 | 
									   iza_beh_==106 |
									   iza_beh_==112 | 
									   iza_beh_==118 | 
									   iza_beh_==119 | 
									   iza_beh_==120 | 
									   iza_beh_==140 |
									   iza_beh_==141 | 
									   iza_beh_==142 | 
									   iza_beh_==143 | 
									   iza_beh_==149 | 
									   iza_beh_==201 | 
									   iza_beh_==203 |
									   iza_beh_==205 )
									    & (iza_asu_~=31 & iza_asu_~=32 & iza_leh_==.)
									    & (iza_mth1_==. )
									    & (iza_mth2_==. )
									    & (iza_mth3_==. )
									    & (iza_mth4_==. ); 
					;
				#delimit cr
				by iza_id: egen iza_sv_tminus1 = sum(sv_tminus1_temp)
			
				/* Full-time employment*/
				gen sv_full_tminus1_temp = 0 
				by iza_id: replace sv_full_tminus1_temp = 1 if iza_azeit_ >= 1 & iza_azeit_ <= 7  	 
				by iza_id: egen sv_full_tminus1 = sum(sv_full_tminus1_temp)
				drop sv_full_tminus1_temp
				/* Months in parttime employment*/
				gen sv_part_tminus1_temp = 0 
				by iza_id: replace sv_part_tminus1_temp = 1 if iza_azeit_ == 8  	 
				by iza_id: egen sv_part_tminus1 = sum(sv_part_tminus1_temp)
				drop sv_part_tminus1_temp
				/* months in reduced fulltime employment */
				gen sv_mid_tminus1_temp = 0 
				by iza_id: replace sv_mid_tminus1_temp = 1 if iza_azeit_ == 9  	 
				by iza_id: egen sv_mid_tminus1 = sum(sv_mid_tminus1_temp)
				drop sv_mid_tminus1_temp
			
				/*log average daily wage*/
				by iza_id: egen wage_tminus1_temp = mean(iza_geld_beh_) if sv_tminus1_temp==1
				by iza_id: egen iza_lnwage_tminus1 = max(wage_tminus1_temp)
				by iza_id: replace iza_lnwage_tminus1 = ln(iza_lnwage_tminus1)
				drop sv_tminus1_temp	wage_tminus1_temp
	
				/*months in unemployment insurance*/
				gen ui_tminus1_temp = 0
				by iza_id: replace ui_tminus1_temp = 1 if ((iza_asu_==31 | iza_asu_==32) & (iza_leh_==1 | iza_leh_==2) | (iza_mth1_==.))
				by iza_id: egen iza_ui_tminus1 = sum(ui_tminus1_temp)
				drop ui_tminus1_temp	
				
				/*Months in unemployment*/
				gen alo_tminus1_temp = 0
				by iza_id: replace alo_tminus1_temp = 1 if (iza_asu_==31 | iza_asu_==32) | iza_leh_~=. | (iza_mth1_==.)
				by iza_id: egen iza_alo_tminus1 = sum(alo_tminus1_temp)
				drop alo_tminus1_temp	
	
				/*Months out of the labour force*/
				gen olf_tminus1_temp = 0
				# del ; 
				by iza_id: replace olf_tminus1_temp = 1 if iza_asu_==.  & iza_beh_==. & iza_leh_==. & iza_lhg_==.
								& (iza_mth1_==. )
								& (iza_mth2_==. )
								& (iza_mth3_==. )
								& (iza_mth4_==. ); 
				# del cr
				by iza_id: egen iza_olf_tminus1 = sum(olf_tminus1_temp)
				drop olf_tminus1_temp	
	
				/*Months in ALMP*/
				gen treatment_tminus1_temp = 0
				#delimit;
				by iza_id: replace treatment_tminus1_temp = 1 if 
								  (iza_mth1_!=. )
								| (iza_mth2_!=. )
								| (iza_mth3_!=. )
								| (iza_mth4_!=. ); 
				#delimit cr
				by iza_id: egen iza_treatment_tminus1 = sum(treatment_tminus1_temp)
				drop treatment_tminus1_temp 	
				
				by iza_id: drop if _n>1
	
				keep iza_id *_tminus1
	
				tempfile tminus1_`year'
				save `tminus1_`year'', replace
				
				restore
	

				/*==================================================================================*/ 
				/*Status 2-5 years before entry into unemployment*/
				/*==================================================================================*/

				
				if `year' <2003 {
				local tminus = `year'-1993
				local x "2/5"
				}
				
				if `year' >=2003 {
				local x "2/5"
				}
			
			
				forvalue t = `x' {

					local help = `t'-1
		
					preserve
		
					/*Keep the relevant time window*/
					by iza_id: gen help1 = eintritt - `help'00  
					by iza_id: keep if lfd < help1
					by iza_id: gen help2 = help1 - 100  
					by iza_id: keep if lfd >= help2
					drop help1 help2
		
					/*Occupational training*/
					gen apprentice_tminus`t'_temp = 0  
					#delimit;
					by iza_id: replace apprentice_tminus`t'_temp = 1 if iza_beh_==102 & (iza_asu_~=31 & iza_asu_~=32 & iza_leh_==.)
											& (iza_mth1_==. )
											& (iza_mth2_==. )
											& (iza_mth3_==. )
											& (iza_mth4_==. ); 
					#delimit cr
					by iza_id: egen iza_apprentice_tminus`t' = max(apprentice_tminus`t'_temp)
					drop apprentice_tminus`t'_temp
		
					/*Employment*/
					gen sv_tminus`t'_temp = 0
					#delimit;
					by iza_id: replace sv_tminus`t'_temp = 1 if (iza_beh_==101 |
										     iza_beh_==102 |
										     iza_beh_==103 | 
										     iza_beh_==105 | 
										     iza_beh_==106 |
										     iza_beh_==112 | 
										     iza_beh_==118 | 
										     iza_beh_==119 | 
										     iza_beh_==120 | 
										     iza_beh_==140 |
										     iza_beh_==141 | 
										     iza_beh_==142 | 
										     iza_beh_==143 | 
										     iza_beh_==149 | 
										     iza_beh_==201 | 
										     iza_beh_==203 |
										     iza_beh_==205 )
										     & (iza_asu_~=31 & iza_asu_~=32 & iza_leh_==.)
										     & (iza_mth1_==. )
										     & (iza_mth2_==. )
										     & (iza_mth3_==. )
										     & (iza_mth4_==. )
				
					;
					#delimit cr
					by iza_id: egen iza_sv_tminus`t' = sum(sv_tminus`t'_temp)
		
					/* Months in fulltime employment*/
					gen sv_full_tminus`t'_temp = 0 
					by iza_id: replace sv_full_tminus`t'_temp = 1 if iza_azeit_ >= 1 & iza_azeit_ <= 7  	 
					by iza_id: egen sv_full_tminus`t' = sum(sv_full_tminus`t'_temp)
					drop sv_full_tminus`t'_temp
					/* Months in parttime employment*/
					gen sv_part_tminus`t'_temp = 0 
					by iza_id: replace sv_part_tminus`t'_temp = 1 if iza_azeit_ == 8  	 
					by iza_id: egen sv_part_tminus`t' = sum(sv_part_tminus`t'_temp)
					drop sv_part_tminus`t'_temp
					/* Months in reduced fulltime employment*/
					gen sv_mid_tminus`t'_temp = 0 
					by iza_id: replace sv_mid_tminus`t'_temp = 1 if iza_azeit_ == 9  	 
					by iza_id: egen sv_mid_tminus`t' = sum(sv_mid_tminus`t'_temp)
					drop sv_mid_tminus`t'_temp
					
					
					/*log average daily wage*/
					by iza_id: egen wage_tminus`t'_temp = mean(iza_geld_beh_) if sv_tminus`t'_temp==1
					by iza_id: egen iza_lnwage_tminus`t' = max(wage_tminus`t'_temp)
					by iza_id: replace iza_lnwage_tminus`t' = ln(iza_lnwage_tminus`t')
					drop sv_tminus`t'_temp	wage_tminus`t'_temp
		
					/*Months in UI*/
					gen ui_tminus`t'_temp = 0
					by iza_id: replace ui_tminus`t'_temp = 1 if ((iza_asu_==31 | iza_asu_==32) & (iza_leh_==1 | iza_leh_==2) | (iza_mth1_==.))
					by iza_id: egen iza_ui_tminus`t' = sum(ui_tminus`t'_temp)
					drop ui_tminus`t'_temp
					
					/*Months in unemployemnt*/
					gen alo_tminus`t'_temp = 0
					by iza_id: replace alo_tminus`t'_temp = 1 if (iza_asu_==31 | iza_asu_==32) | iza_leh_~=. | (iza_mth1_==. )
					by iza_id: egen iza_alo_tminus`t' = sum(alo_tminus`t'_temp)
					drop alo_tminus`t'_temp	
		
					/*Months out of the labour force*/
					gen olf_tminus`t'_temp = 0
					# del; 
					by iza_id: replace olf_tminus`t'_temp = 1 if iza_asu_==. & iza_beh_==. & iza_leh_==. & iza_lhg_==.
										& (iza_mth1_==.) 
										& (iza_mth2_==.) 
										& (iza_mth3_==.) 
										& (iza_mth4_==.) ;
					# del cr 
					by iza_id: egen iza_olf_tminus`t' = sum(olf_tminus`t'_temp)
					drop olf_tminus`t'_temp	
		
					/*Months in ALMP*/
					gen treatment_tminus`t'_temp = 0
					#delimit;
					by iza_id: replace treatment_tminus`t'_temp = 1 if 
								  (iza_mth1_ !=. )
								| (iza_mth2_ !=. )
								| (iza_mth3_ !=. )
								| (iza_mth4_ !=. );
					#delimit cr
					by iza_id: egen iza_treatment_tminus`t' = sum(treatment_tminus`t'_temp)
					drop treatment_tminus`t'_temp	
		
					by iza_id: drop if _n>1
		
					keep iza_id *_tminus`t'
		
					tempfile tminus`t'_`year'
					save `tminus`t'_`year'', replace
		
					restore
		
				} /*ende: t*/
	
	
				/*Generate one data set*/
	
				use `tminus1_`year'', clear
	
				forvalue t = `x' {
	
					joinby iza_id using `tminus`t'_`year'', unmatched(both)
					tab _merge
					drop _merge
		
				} /*end: t*/
				
				
				compress				
				saveold  $data/history`help_out'_`year'_`iebversion', replace
				erase $data/hist_`year'.dta
				erase $data/hist_`year1'.dta
				erase $data/hist_`year2'.dta
				erase $data/hist_`year3'.dta
				erase $data/hist_`year4'.dta
				erase $data/hist_`year5'.dta
				
			}/* year*/
			
		}/* out*/
	}/* ieb-version*/
	
	
log close

